#include "../header.h"
/**
 * @函数原型:  int dup(int oldfd);
 * @函数功能: 复制文件描述符
 * @参数说明: 
 * @       参数1: oldfd 旧文件描述符
 * @返回值: 新文件描述符
 * @param {int} argc
 */
/**
 * @函数原型:  int dup2(int oldfd, int newfd);
 * @函数功能: 复制文件描述符- 指定一个自定义文件描述符
 * @参数说明: 
 * @       参数1: oldfd 旧文件描述符
 * @       参数2: newfd 指定的新文件描述符
 * @       参数3: 
 * @返回值: 新文件描述符
 * On success, these system calls return the new file descriptor.  On error, -1 is returned, and errno is set ap‐
       propriately.
 * @param {int} argc
 */
int main(int argc, char const *argv[])
{
    int fd = open("hello.data",O_CREAT | O_RDWR | O_TRUNC,0666);
    printf("fd = %d\n",fd);

    const char *str = "hello linux\n";
    write(fd,str,strlen(str));

    //? fd 与fd1 共同指向 同一个文件
    int fd1 = dup(fd);
    printf("fd1 = %d\n",fd1);
    const char * str1 = "good job\n" ;
    write(fd1,str1,strlen(str1));

    //? dup2:指定文件描述符
    int fd2 = dup2(fd1,100);
    printf("fd2 = %d\n",fd2);
    const char * str2 = "sunday!\n" ;
    write(fd2,str2,strlen(str2));

    return 0;
}
